/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-17 16:09:04
 */
import { reactive } from 'vue';
import { removeUndefinedParams } from '@/utils/index';
import { SearchOptionsType, CPageTableProps, ColumnSettingTypeEnum } from '@/components/CPageTable';
import type { CTableProps } from '@/components/CTable';
import { getRoleList } from '@/apis/system/role';
import { GetRolesListParams } from '@/apis/system/model/roleModel';
import { BasicPagingListParams } from '@/apis/basicModel';

export const useRoleList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetRolesListParams>({
    roleName: ''
  });

  // 搜索配置
  const searchOptions: SearchOptionsType[] = [
    {
      name: '角色名称',
      field: 'roleName',
      type: 'input',
      placeholder: '请输入角色名称'
    }
  ];

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  };

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter);
    return getRoleList(params);
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      roleName: searchForm.roleName
    };
    params = removeUndefinedParams(params);
    return params;
  };

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '角色名称',
      field: 'roleName',
      minWidth: '100',
      slot: 'roleName',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '角色描述',
      field: 'roleDesc',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建者',
      field: 'creator',
      width: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建时间',
      field: 'createTime',
      width: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'op',
      width: '190',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,

    tableFieldsSetting
  };
};
